package com.feonix.complat.controller.role;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.feonix.common.annotation.Permission;
import com.feonix.common.exception.OperationException;
import com.feonix.common.util.StringUtil;
import com.feonix.common.util.mvc.JsonResult;
import com.feonix.common.util.mvc.ResultState;
import com.feonix.common.view.tree.Tree;
import com.feonix.common.view.tree.TreeNode;
import com.feonix.complat.entity.Right;
import com.feonix.complat.entity.Role;
import com.feonix.complat.formbean.RightFormBean;
import com.feonix.complat.formbean.RoleFormBean;
import com.feonix.complat.service.RightService;
import com.feonix.complat.service.RoleService;

/**
 * 
 * @CopyRight (c)1993-2017
 * @Project m-complat-controller
 * @Comments 角色菜单操作类
 * @NameSpace com.feonix.complat.controller.role
 * @Author Feonixzw
 * @CreateDate 2017年12月20日
 */
@Controller
@Permission(module = "right")
@RequestMapping("manager/main/right")
public class OprRightController {

	@Autowired
	private RightService rightService;

	/**
	 * 打开新增页面
	 * 
	 * @return
	 */
	@Permission(function = "add_show")
	@RequestMapping(value = "add_show")
	public ModelAndView showAdd() {
		ModelAndView modelAndView = new ModelAndView("complat/right/right_opr");
		modelAndView.addObject("right", new Right());
		modelAndView.addObject("url", "add_submit.do");
		return modelAndView;
	}

	/**
	 * 打开编辑页面
	 * 
	 * @param iid
	 *            角色ID
	 * @return
	 */
	@Permission(function = "modify_show")
	@RequestMapping(value = "modify_show")
	public ModelAndView showModify(int iid) {
		ModelAndView modelAndView = new ModelAndView("complat/right/right_opr");
		Right right = rightService.findByIid(iid);
		modelAndView.addObject("url", "modify_submit.do");
		modelAndView.addObject("right", right);
		return modelAndView;
	}

	/**
	 * 提交新增
	 * 
	 * @param roleFormBean
	 *            角色FormBean
	 * @return
	 */
	@Permission(function = "add_submit")
	@RequestMapping(value = "add_submit")
	@ResponseBody
	public JsonResult submitAdd(RightFormBean rightFormBean) {
		boolean isSuccess = false;
		JsonResult jsonResult = JsonResult.getInstance();
		try {
			isSuccess = rightService.add(rightFormBean);
			if (isSuccess) {
				jsonResult.set(ResultState.ADD_SUCCESS);
			} else {
				jsonResult.set(ResultState.ADD_FAIL);
			}
		} catch (OperationException e) {
			jsonResult.setMessage(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 提交修改
	 * 
	 * @param roleFormBean
	 *            角色FormBean
	 * @return
	 */
	@Permission(function = "modify_submit")
	@RequestMapping(value = "modify_submit")
	@ResponseBody
	public JsonResult submitModify(RightFormBean rightFormBean) {
		boolean isSuccess = false;
		JsonResult jsonResult = JsonResult.getInstance();
		try {
			isSuccess = rightService.modify(rightFormBean);
			if (isSuccess) {
				jsonResult.set(ResultState.MODIFY_SUCCESS);
			} else {
				jsonResult.set(ResultState.MODIFY_FAIL);
			}
		} catch (OperationException e) {
			jsonResult.setMessage(e.getMessage());
		}

		return jsonResult;
	}

	/**
	 * 角色删除
	 * 
	 * @param ids
	 *            角色ID串 如:1,2,3,4
	 * @return
	 */
	@Permission(function = "remove")
	@RequestMapping(value = "remove")
	@ResponseBody
	public JsonResult remove(String ids) {
		boolean isSuccess = false;
		JsonResult jsonResult = JsonResult.getInstance();
		try {
			isSuccess = rightService.removeByIds(ids);
			if (isSuccess) {
				jsonResult.set(ResultState.REMOVE_SUCCESS);
			} else {
				jsonResult.set(ResultState.REMOVE_FAIL);
			}
		} catch (OperationException e) {
			jsonResult.setMessage(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 显示角色权限关系界面
	 * 
	 * @param iid
	 *            角色ID
	 * @return
	 */
	@Permission(function = "righttree_show")
	@RequestMapping(value = "righttree_show")
	public ModelAndView showRightTree() {
		ModelAndView modelAndView = new ModelAndView("complat/right/righttree_list");
		List<Right> allRightList = rightService.findAllRights();
		Tree roleRightTree = Tree.getInstance("rightId", "rightName");
		roleRightTree.addNode(TreeNode.getInstance("ALLPARENT", "", "角色权限").setIsDisabled(true));
		for (Right right : allRightList) {
			// 只有父级机构给选择
			if (StringUtil.equals(right.getParentIid(), "ALLPARENT")) {
				roleRightTree.addNode(TreeNode
						.getInstance(StringUtil.getString(right.getIid()), StringUtil.getString(right.getParentIid()),
								StringUtil.isEmpty(right.getFunctionId()) ? right.getModuleName()
										: right.getFunctionName())
						.addAttr("codeId", right.getCodeId()).addAttr("moduleId", right.getModuleId())
						.addAttr("moduleName", right.getModuleName()));
			} else {
				roleRightTree.addNode(TreeNode.getInstance(StringUtil.getString(right.getIid()),
						StringUtil.getString(right.getParentIid()),
						StringUtil.isEmpty(right.getFunctionId()) ? right.getModuleName() : right.getFunctionName())
						.setIsDisabled(true));
			}

		}
		modelAndView.addObject("tree", roleRightTree.parse());
		return modelAndView;
	}

}
